package com.woniu.coupon.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.woniu.coupon.domain.Coupon;
import com.woniu.coupon.service.ICouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 * 
 * @author yy
 * @date 2023-04-03
 */
@RestController
@RequestMapping("/coupon/tbcoupon")
@Api("优惠券管理")
public class CouponController extends BaseController
{
    @Autowired
    private ICouponService couponService;

    /**
     * 查询优惠券列表
     */
    //@PreAuthorize("@ss.hasPermi('coupon:tbcoupon:list')")
    @GetMapping("/list")
    @ApiOperation("优惠券分页查询")
    public TableDataInfo list(Coupon coupon)
    {
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    //@PreAuthorize("@ss.hasPermi('coupon:tbcoupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("优惠券导出")
    public void export(HttpServletResponse response, Coupon coupon)
    {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    //@PreAuthorize("@ss.hasPermi('coupon:tbcoupon:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("根据主键查询优惠券")
    @ApiImplicitParam(name = "ids", value = "用户ID", required = true, dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(couponService.selectCouponById(id));
    }




    @RequestMapping("/feign/getCouponById")
    public Coupon getCouponById(@RequestParam("id") Long id)
    {
        return couponService.selectCouponById(id);
    }

    /**
     * 新增优惠券
     */
    //@PreAuthorize("@ss.hasPermi('coupon:tbcoupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加优惠券")
    public AjaxResult add(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券
     */
    //@PreAuthorize("@ss.hasPermi('coupon:tbcoupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("更新优惠券")
    public AjaxResult edit(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券
     */
    //@PreAuthorize("@ss.hasPermi('coupon:tbcoupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除优惠券")
    @ApiImplicitParam(name = "ids", value = "主键数组", required = true, dataType = "Long")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(couponService.deleteCouponByIds(ids));
    }
}
